<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文件名称" prop="fileName">
        <el-input v-model="queryParams.fileName" placeholder="请输入文件名称" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="文件类型" prop="fileType">
        <el-select v-model="queryParams.fileType" placeholder="请选择文件类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.FILE_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="落实状态" prop="implStatus">
        <el-select v-model="queryParams.implStatus" placeholder="请选择落实状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.IMPL_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRangeCreateTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['biz:document-impl:create']">新增</el-button>
      </el-col>
       <el-col :span="1.5">
        <el-button type="info" icon="el-icon-upload2" size="mini" @click="handleImport">导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="文件名称" align="center" prop="fileName" />
      <el-table-column label="文件类型" align="center" prop="fileType">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.FILE_TYPE" :value="scope.row.fileType" />
        </template>
      </el-table-column>
      <el-table-column label="落实要求" align="center" prop="implRequirement" />
      <el-table-column label="落实状态" align="center" prop="implStatus">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.IMPL_STATUS" :value="scope.row.implStatus" />
        </template>
      </el-table-column>
      <el-table-column label="反馈附件" align="center" prop="feedbackAttach">
        <template slot-scope="scope">
          <span v-if="scope.row.feedbackAttach == '0' || scope.row.feedbackAttach == null">
            未上传
          </span>
          <span v-if="scope.row.feedbackAttach == '1'">
            <el-button type="primary" size="mini" @click="showAttach(scope.row)">查看持续工作反馈附件</el-button>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['biz:document-impl:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['biz:document-impl:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件名称" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入文件名称" />
        </el-form-item>
        <el-form-item label="文件类型" prop="fileType">
          <el-select v-model="form.fileType" placeholder="请选择文件类型">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.FILE_TYPE)"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="落实要求" prop="implRequirement">
          <el-input v-model="form.implRequirement" placeholder="请输入落实要求" />
        </el-form-item>
        <el-form-item label="落实状态" prop="implStatus">
          <el-select v-model="form.implStatus" placeholder="请选择落实状态">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.IMPL_STATUS)"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="文件附件" prop="ossDOList">
          <FileUpload v-model="form.ossDOList"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 文件列表 -->
    <el-dialog :title="attachTitle" :visible.sync="attachOpen" width="700px" append-to-body>
      <el-table v-loading="attachLoading" :data="attachList">
      <el-table-column label="附件名称" align="center" prop="originalName">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" v-html="scope.row.originalName" @click="handleDownload(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-dialog>

    <!-- 文件落实导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click='downloadTemplate'>下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createDocumentImpl, updateDocumentImpl, deleteDocumentImpl, getDocumentImpl, getDocumentImplPage, exportDocumentImplExcel,showAttach,downloadTemplate } from "@/api/biz/documentImpl";
import {getBaseHeader} from "@/utils/request";
export default {
  name: "DocumentImpl",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件落实列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        fileName: null,
        fileType: null,
        implStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //附件遮罩层
      attachLoading: true,
      //附件list
      attachList:[],
      //附件弹窗标题
      attachTitle:"",
      attachOpen:false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: getBaseHeader(),
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/admin-api/' + "/biz/document-impl/import"
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getDocumentImplPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        fileName: undefined,
        fileType: undefined,
        implRequirement: undefined,
        implStatus: undefined,
        status: undefined,
        filePath: undefined,
        tenantId: undefined,
        deptId: undefined,
        ossDOList:[]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文件落实";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getDocumentImpl(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文件落实";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        var formPath = this.form.ossDOList;
        let path = "";
        for(var i=0;i<formPath.length;i++){
          if(formPath[i].id!==undefined){
            path += formPath[i].id+",";
          }
        }
        path = path.substring(0,path.lastIndexOf(','))
        this.form.filePath = path;
        // 修改的提交
        if (this.form.id != null) {
          updateDocumentImpl(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createDocumentImpl(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除文件落实编号为"' + id + '"的数据项?').then(function() {
          return deleteDocumentImpl(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm('是否确认导出所有文件落实数据项?').then(() => {
          this.exportLoading = true;
          return exportDocumentImplExcel(params);
        }).then(response => {
          this.$download.excel(response, '${table.classComment}.xls');
          this.exportLoading = false;
        }).catch(() => {});
    },
    //查看持续工作反馈附件
    showAttach(row){
      this.attachTitle="附件列表";
      this.attachOpen = true;
      showAttach(row.filePath).then(response=>{
        this.attachList = response.data;
        this.attachLoading = false;
      });
    },
    /** 下载按钮操作 */
    handleDownload(id) {
      this.$downloadOss.oss(id)
    },
    /** 下载模板操作 */
    downloadTemplate() {
      downloadTemplate().then(response => {
        this.$download.excel(response, '文件落实导入模板.xls');
      });
    },
     /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "文件落实导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      if (response.code !== 0) {
        this.$modal.msgError(response.msg)
        return;
      }
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      // 拼接提示语
      let data = response.data;
      let text = '';
      text += '<br />更新失败数量：' + Object.keys(data.failureDocumentnames).length;
      for (const documentname in data.failureDocumentnames) {
        text += '<br />&nbsp;&nbsp;&nbsp;&nbsp;' + documentname + '：' + data.failureDocumentnames[documentname];
      }
      this.$alert(text, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>
